﻿function InitQueryPage(parameter) {
   
    var ControlStr = $.ajax({
        type: "GET",
        url: "../Common/CustomQueryInfo.aspx",
        data: parameter,
        timeout: 5000,
        cache: false,
        async: false
    }).responseText;

    if (ControlStr.indexOf("Error") == 0) {
        var div = document.getElementById("div_query");
        var inputs = div.getElementsByTagName("input");
        var selects = div.getElementsByTagName("select");
        
        var inputCount = inputs.length;
        var selectCount = (selects.length - inputCount) / 2; 
        
        var totalCount = inputCount + selectCount;
        autoHeight(totalCount);
        return;
    } 
    else 
    {
        var ControlArray = ControlStr.split("|");
        var tempTable = $(document.createElement("div"));
        $("#div_query table").clone(true).appendTo(tempTable);
        $("#div_query tr").remove(); //清空原始table数据
         try {
                for (j = 0; j < ControlArray.length; j++) 
                {
                    $.each(tempTable.find("tr"), function(i, n) {
                            var Controls = ControlArray[j].split(",");
                            var Laber = Controls[0];
                            var Control1 = Controls[1];

                            if (Controls.length > 2) {
                                var Control2 = Controls[2];
                            }

                            if ($(n).find("#" + Control1.split(":")[0]).attr("name") == Control1.split(":")[0] || $(n).find("#" + Laber.split(":")[0]).attr("id") == Laber.split(":")[0]) {
                                if (Control1.split(":")[1].length > 0 && $(n).find("#" + Control1.split(":")[0]).html() != null) {//设置控件1初始值
                                    if (Control1.split(":")[0].substr(0, 3) == "txt") {
                                        $(n).find("#" + Control1.split(":")[0]).val(Control1.split(":")[1]);
                                    } else {
                                        $(n).find("#" + Control1.split(":")[0]).setSelectedValue(Control1.split(":")[1]);
                                    }
                                }
                                if (Control2 !="") {
                                    if (Control2.split(":")[1].length > 0 && $(n).find("#" + Control1.split(":")[0]).html() != null) {//设置控件2初始值
                                        if (Control2.split(":")[0].substr(0, 3) == "txt") {
                                            $(n).find("#" + Control2.split(":")[0]).val(Control2.split(":")[1]);
                                        } else {
                                            $(n).find("#" + Control2.split(":")[0]).setSelectedValue(Control2.split(":")[1]);
                                        }
                                    }
                                }

                                $(n).appendTo($("#autodiv_input_query table"));
                            }
                        
                    });
                }
        }
        catch (err) {
            alert("Initialize data failed, please try it again");
            return false;   
        }
        autoHeight(ControlArray.length)
    }
}

//根据客户端浏览器分辨率调整Panel高度        
function autoHeight(count)
{    
    if( count >= 13 )
    {
       var x = window.screen.width.toString();
       var y = window.screen.height.toString();
       if(x == "800" && y == "600")
       {
            document.getElementById("div_query").style.height="400px";       
       }
       else if(x == "1024" && y == "768")
       {
            document.getElementById("div_query").style.height="483px";
       }
       else if(x > "1024" && x < "1280" && y > "768" && y < "800")  //1240*768 - 1280*800之间
       {
            document.getElementById("div_query").style.height="490px";
       }
       else if(x == "1280" && y == "800")
       {
            document.getElementById("div_query").style.height="500px";
       }
       else if(x == "1280" && y == "1024")
       {
            document.getElementById("div_query").style.height="550px";
       }
     }
     else
     {
        document.getElementById("div_query").style.height="auto";
     }
}
